package com.bowlong.third.redis.lang;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import redis.clients.jedis.Jedis;

import com.alibaba.fastjson.JSON;

@SuppressWarnings("unchecked")
public class JedisMap<K, V> implements Map<K, V> {
	final Jedis jedis;
	final String pkey;

	public JedisMap(Jedis jedis, String pkey) {
		this.jedis = jedis;
		this.pkey = pkey;
	}

	@Override
	public int size() {
		return this.jedis.hlen(pkey).intValue();
	}

	@Override
	public boolean isEmpty() {
		return size() <= 0;
	}

	@Override
	public boolean containsKey(Object key) {
		String key2 = JSON.toJSONString(key);
		return this.jedis.hexists(pkey, key2);
	}

	@Override
	public boolean containsValue(Object value) {
		String tex = JSON.toJSONString(value);
		Map<String, String> texs = this.jedis.hgetAll(tex);
		Set<java.util.Map.Entry<String, String>> ens = texs.entrySet();
		for (Entry<String, String> e : ens) {
			String val = e.getValue();
			if (val.equals(tex))
				return true;
		}
		return false;
	}

	@Override
	public V get(Object key) {
		String key2 = JSON.toJSONString(key);
		String text = this.jedis.hget(pkey, key2);
		if (text == null || text.isEmpty())
			return null;
		return (V) JSON.parse(text);
	}

	@Override
	public V put(K key, V value) {
		String key2 = JSON.toJSONString(key);
		String text = JSON.toJSONString(value);
		this.jedis.hset(pkey, key2, text);
		return value;
	}

	@Override
	public V remove(Object key) {
		String key2 = JSON.toJSONString(key);
		String text = this.jedis.hget(pkey, key2);
		if (text == null || text.isEmpty())
			return null;
		V r2 = (V) JSON.parse(text);
		this.jedis.hdel(pkey, key2);
		return r2;
	}

	@Override
	public void putAll(Map<? extends K, ? extends V> m) {
		Map<String, String> hash = new HashMap<>();
		Set<? extends K> keys = m.keySet();
		for (K key : keys) {
			V value = m.get(key);
			String key2 = JSON.toJSONString(key);
			String text = JSON.toJSONString(value);
			hash.put(key2, text);
		}
		this.jedis.hmset(pkey, hash);
	}

	@Override
	public void clear() {
		this.jedis.del(pkey);
	}

	@Override
	public Set<K> keySet() {
		Set<K> r2 = new HashSet<>();
		Set<String> keys = this.jedis.hkeys(pkey);
		for (String text : keys) {
			K key = (K) JSON.parse(text);
			r2.add(key);
		}
		return r2;
	}

	@Override
	public Collection<V> values() {
		List<V> r2 = new Vector<>();
		List<String> values = this.jedis.hvals(pkey);
		for (String text : values) {
			V val = (V) JSON.parse(text);
			r2.add(val);
		}
		return r2;
	}

	@Override
	public Set<java.util.Map.Entry<K, V>> entrySet() {
		Map<K, V> map = new HashMap<>();
		Map<String, String> datas = this.jedis.hgetAll(pkey);
		Set<java.util.Map.Entry<String, String>> ens = datas.entrySet();
		for (Entry<String, String> entry : ens) {
			String skey = entry.getKey();
			String text = entry.getValue();

			K key = (K) JSON.parse(skey);
			V val = (V) JSON.parse(text);
			map.put(key, val);
		}
		return map.entrySet();
	}

}
